// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2024 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.13.1
package sys

import (
	"context"
	"github.com/gogf/gf/v2/os/gtime"
	"hotgo/api/admin/stockztpool"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/tradeDate"
	"sort"
)

var (
	StockZtPool = cStockZtPool{}
)

type cStockZtPool struct{}

// List 查看涨停表现列表
func (c *cStockZtPool) List(ctx context.Context, req *stockztpool.ListReq) (res *stockztpool.ListRes, err error) {
	list, totalCount, err := service.SysStockZtPool().List(ctx, &req.StockZtPoolListInp)
	if err != nil {
		return
	}

	if list == nil {
		list = []*sysin.StockZtPoolListModel{}
	}

	res = new(stockztpool.ListRes)
	res.List = list
	res.PageRes.Pack(req, totalCount)
	return
}

func (c *cStockZtPool) ZtInfo(ctx context.Context, req *stockztpool.ZtInfoReq) (res *stockztpool.ListZtInfoRes, err error) {

	listInp := sysin.StockZtPoolListInp{
		PageReq: form.PageReq{
			Page:    1,
			PerPage: 1000,
		},
		Date: req.Date,
	}
	list, _, err := service.SysStockZtPool().List(ctx, &listInp)
	if err != nil {
		return
	}

	if list == nil {
		list = []*sysin.StockZtPoolListModel{}
	}
	ztInfoRespList := c.formatZTInfo(list)
	res = new(stockztpool.ListZtInfoRes)
	res.List = ztInfoRespList
	return
}

// LTJJ 龙头晋级
func (c *cStockZtPool) LTJJ(ctx context.Context, req *stockztpool.LTJJInfoReq) (res *stockztpool.ListJJInfoRes, err error) {

	todayInp := sysin.StockZtPoolListInp{
		PageReq: form.PageReq{
			Page:    1,
			PerPage: 1000,
		},
		Date: req.Date,
	}
	todayList, _, err := service.SysStockZtPool().List(ctx, &todayInp)
	if err != nil {
		return
	}

	if todayList == nil {
		todayList = []*sysin.StockZtPoolListModel{}
	}
	prevTradeDay := tradeDate.GetStartTradeDay(gtime.New(req.Date).Format("Y-m-d"), 1)
	prevInp := sysin.StockZtPoolListInp{
		PageReq: form.PageReq{
			Page:    1,
			PerPage: 1000,
		},
		Date: prevTradeDay,
	}
	prevList, _, err := service.SysStockZtPool().List(ctx, &prevInp)
	if err != nil {
		return
	}

	if prevList == nil {
		prevList = []*sysin.StockZtPoolListModel{}
	}
	ztInfoRespList := c.formatZTInfo(todayList)
	prevTtInfoRespList := c.formatZTInfo(prevList)
	res = new(stockztpool.ListJJInfoRes)
	res.List = ztInfoRespList
	res.PrevList = prevTtInfoRespList
	return
}

func (c *cStockZtPool) formatZTInfo(todayList []*sysin.StockZtPoolListModel) []*stockztpool.ZtInfoResp {
	formatList := make(map[string][]*sysin.StockZtPoolListModel, len(todayList))
	for _, v := range todayList {
		if v.ChangePercent < 0 {
			continue
		}
		name := ""
		switch v.LbNumber {
		case 1:
			name = "首板"
		case 2:
			name = "二板"
		case 3:
			name = "三板"
		case 4:
			name = "四板"
		case 5:
			name = "五板"
		case 6:
			name = "六板"
		case 7:
			name = "七板"
		case 8:
			name = "八板"
		case 9:
			name = "九板"
		case 10:
			name = "十板"
		case 11:
			name = "十一板"
		case 12:
			name = "十二板"
		case 13:
			name = "十三板"
		case 14:
			name = "十四板"
		case 15:
			name = "十五板"
		case 16:
			name = "十六板"
		case 17:
			name = "十七板"
		case 18:
			name = "十八板"
		case 19:
			name = "十九板"
		case 20:
			name = "二十板"
		case 21:
			name = "二十一板"
		case 22:
			name = "二十二板"
		default:
			name = ""
		}
		formatList[name] = append(formatList[name], v)
	}
	ztInfoRespList := make([]*stockztpool.ZtInfoResp, 0)
	for name, models := range formatList {
		ztInfoRespList = append(ztInfoRespList, &stockztpool.ZtInfoResp{
			Name: name,
			List: models,
		})
	}
	sort.Slice(ztInfoRespList, func(i, j int) bool {
		return ztInfoRespList[i].List[0].LbNumber > ztInfoRespList[j].List[0].LbNumber
	})
	return ztInfoRespList
}

// Export 导出涨停表现列表
func (c *cStockZtPool) Export(ctx context.Context, req *stockztpool.ExportReq) (res *stockztpool.ExportRes, err error) {
	err = service.SysStockZtPool().Export(ctx, &req.StockZtPoolListInp)
	return
}

// Edit 更新涨停表现
func (c *cStockZtPool) Edit(ctx context.Context, req *stockztpool.EditReq) (res *stockztpool.EditRes, err error) {
	err = service.SysStockZtPool().Edit(ctx, &req.StockZtPoolEditInp)
	return
}

// View 获取指定涨停表现信息
func (c *cStockZtPool) View(ctx context.Context, req *stockztpool.ViewReq) (res *stockztpool.ViewRes, err error) {
	data, err := service.SysStockZtPool().View(ctx, &req.StockZtPoolViewInp)
	if err != nil {
		return
	}

	res = new(stockztpool.ViewRes)
	res.StockZtPoolViewModel = data
	return
}

// Delete 删除涨停表现
func (c *cStockZtPool) Delete(ctx context.Context, req *stockztpool.DeleteReq) (res *stockztpool.DeleteRes, err error) {
	err = service.SysStockZtPool().Delete(ctx, &req.StockZtPoolDeleteInp)
	return
}
